<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// 
//  fetch_vipshop.php
//  www.6block.com
//  
//  Created by Will_Lee on 2011-06-02.
//  Copyright 2011 lishuzu. All rights reserved.
// 

/**
* 抓取vipshop数据
*/
class Ihush_model extends CI_Model
{
	
	function __construct()
	{
		parent::__construct();
		$this->load->library('Snoopy');
		$this->load->library('simple_html_dom', '', 'dom');
		$this->snoopy->agent = 'Mozilla/5.0 (Windows NT 5.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1';
		$this->snoopy->referer = 'http://www.baidu.com';
		//超时 设置为不超时
		$this->snoopy->read_timeout = 0;
		$this->snoopy->rawheaders["Pragma"] = "no-cache";
		$this->snoopy->rawheaders["X_FORWARDED_FOR"] = '220.181.111.147';
		//抓取商品数量
		$this->fetch_goods_num = 6;
		//活动表
		$this->activity_table = 'ihush_activity';
		//商品表
		$this->goods_table = 'ihush_goods';
		//抓取网站链接
		$this->fetchurl = 'http://www.ihush.com/';
	}
	
	function fetch(){
		$activity = array();
		$this->snoopy->fetch($this->fetchurl);
		$html = $this->snoopy->results;
		$this->dom->load($html);
		if ($this->dom->find('ul[class=barnd_list]', 0)) {
			//处理首页链接和相关信息
			foreach ($this->dom->find('ul[class=barnd_list]', 0)->find('li') as $item) {
				if ($item) {
					foreach ($item->find('a') as $act) {
						//这里不是product_list.php 少了p字母 未了放返回值是1
						if (strpos($act->href, 'roduct_list.php')) {
							$actinfo['url'] = $act->href;
									
							if ($act->find('img', 0)) {
								$act_img = $act->find('img', 0)->src;
								$img_file = pathinfo($act_img);
							}else{
								$act_img = '';
								continue;
							}				
							if ($img_file['basename'] == 'blank.gif') {
								//如果应用了延迟加载图片 则加载延迟加载链接
								$imageurl = $act->find('img', 0)->lazy_src;
							}else{
								$imageurl = $act_img;
							}
							
							$actinfo['imageurl'] = $imageurl;							
							$actinfo['title'] = $act->find('img', 0)->title;
							$activies[] = $actinfo;
						}
					}
				}
			}
			foreach ($activies as $activity) {
				if ($activity['url']) {
					$query = $this->db->get_where($this->activity_table, array('url' => $this->fetchurl.$activity['url']));
					$exist_act = $query->row_array();
					if ($exist_act) {
						$logstr = "id:".$exist_act['aid'].";url:".$exist_act['url'].";msg:已经存在抓取失败;";
						writelog('fetch_ihush', $logstr);
					}else{
						//活动链接增加ihush链接
						$activity['url'] = $this->fetchurl.$activity['url'];
						//活动开始时间抓取不到 用现在时间代替
						//starttime
						$activity['starttime'] = now();
						$activity['endtime'] = now();						
						$activity['dateline'] = now();
						$aid = $this->insert($activity);
						//抓取商品信息					
						$this->snoopy->fetch($activity['url']);
						$goodshtml = $this->snoopy->results;
						$this->load->library('simple_html_dom', '', 'goodsdom');
						$this->goodsdom->load($goodshtml);
						if ($this->goodsdom->find('ul[id=productList]', 0)) {
							foreach ($this->goodsdom->find('ul[id=productList]', 0)->find('li') as $key=>$goodsinfo) {
								$img_file = array();
								//限定抓取商品数量
								if ($key == $this->fetch_goods_num) {
									break;
								}
								$goods = $img_file = array();
								
								$goods['aid'] = $aid;
								//商品链接
								$goodsurl = $goodsinfo->find('div[class=prd_title]', 0)->find('a', 0)->href;
								$goods['url'] = $goodsurl ? $this->fetchurl.$goodsurl : '';
								
								//图片地址							
								if ($goodsinfo->find('div[class=prd_img]', 0)) {
									$goods_img = $goodsinfo->find('div[class=prd_img]', 0)->find('img', 1)->src;
									$img_file = pathinfo($goods_img);
								}else{
									$goods_img = '';
									continue;
								}				
								if ($img_file['basename'] == 'blank.gif') {
									//如果应用了延迟加载图片 则加载延迟加载链接
									$imageurl = $goodsinfo->find('div[class=prd_img]', 0)->find('img', 1)->lazy_src;
								}else{
									$imageurl = $goods_img;
								}
								$goods['imageurl'] = $imageurl;
								
								//货物名称
								$goods_name = $goodsinfo->find('div[class=prd_title]', 0)->find('a', 0)->plaintext;
								$goods['name'] = $goods_name ? iconv('GBK', 'UTF-8', $goods_name) : '';
								
								//实际价格
								$price = $goodsinfo->find('div[class=prd_price]', 0)->find('span[class=now]', 0)->plaintext;
								$preg = '/[0-9]*\.?[0-9]+/';
								if(preg_match($preg, $price, $matches)){
									$goods['price'] = intval($matches[0]);
								}else{
									$goods['price'] = '';
								}
								
								//市场价格
								$marketprice = $goodsinfo->find('div[class=prd_price]', 0)->find('span[class=original]', 0)->plaintext;
								$goods['marketprice'] = $marketprice ? intval($marketprice) : '';
								
								//折扣
								$discount = $goodsinfo->find('div[class=prd_price]', 0)->find('span[class=discount]', 0)->plaintext;
								$preg = '/\(([-+]?[0-9]*\.?[0-9]+).+\)?/';
								if(preg_match($preg, $discount, $matches)){
									$discount = $matches[1];
								}else{
									$discount = round(($goods['price'] / $goods['marketprice'])*10, 1);
								}
								$goods['discount'] = $discount ? $discount : '';
								$goods['dateline'] = now();
								
								//商品入库
								$goodsid = $this->insert_goods($goods);
								if ($goodsid) {
									$logstr = "aid:".$goods['aid'].";goodsid:".$goodsid.";url:".$goods['url'].";msg:商品抓取成功;";
									writelog('fetch_ihush', $logstr);
								}else {
									$logstr = "aid:".$goods['aid'].";url:".$goods['url'].";msg:商品抓取失败;";
									writelog('fetch_ihush', $logstr);
								}

								//释放变量
								unset($this->goodsdom);
							}
						}					
						if ($aid) {
							$logstr = "aid:".$aid.";url:".$activity['url'].";msg:活动抓取成功;";
							writelog('fetch_ihush', $logstr);
						}else {
							$logstr = "url:".$activity['url'].";msg:活动抓取失败;";
							writelog('fetch_ihush', $logstr);
						}
					}

				}
			}
			//抓取活动链接和图片结束
			return true;		
		}else{
			return false;
		}


		
	}
	
	function insert($data){
		if (is_array($data)) {
			$this->db->insert($this->activity_table, $data);
			return $this->db->insert_id();
		}
		return false;
	}
	
	function insert_goods($data){
		if (is_array($data)) {
			$this->db->insert($this->goods_table, $data);
			return $this->db->insert_id();
		}
		return false;
	}

	
	
	function get_activitys($num = '', $offset = ''){

		$this->db->order_by('starttime', 'desc');
		
		if (empty($num) || $offset ==='') {
			$this->db->limit('100');
			$query = $this->db->get($this->activity_table);	
		}else{
			$query = $this->db->get($this->activity_table, $num, $offset);				
		}
		
		foreach ($query->result_array() as $row) {
			$row['ison'] = $row['status'] ? '已上架' : '未上架';
			$activitys[] = $row;
		}
		return $activitys;
	}
}

?>